package com.simp.controller;

import com.simp.aop.SysLog;
import com.simp.dto.SysUserDto;
import com.simp.entity.SysUserEntity;
import com.simp.model.SysUserModel;
import com.simp.page.R;
import com.simp.service.SysUserService;
import com.simp.utils.BeanConvertUtils;
import com.simp.vo.SysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author pan.shun
 * @since 2022/3/1 15:39
 */
@Api(tags = "系统用户")
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "用户登录", notes = "用户登录", httpMethod = "POST")
    @PostMapping("/login")
    public R login(@RequestBody SysUserModel sysUser, HttpServletRequest request) {
        SysUserEntity convert = BeanConvertUtils.convert(sysUser, SysUserEntity.class);
        return sysUserService.login(request, convert);
    }

    @ApiOperation(value = "退出登录", notes = "退出登录", httpMethod = "GET")
    @GetMapping("/loginOut")
    public R loginOut(HttpServletRequest request) {
        return sysUserService.loginOut(request);
    }

    @ApiOperation(value = "注册新用户", notes = "注册新用户", httpMethod = "POST")
    @PostMapping("/registerUser")
    public R registerUser(@RequestBody SysUserModel sysUser) {
        SysUserEntity convert = BeanConvertUtils.convert(sysUser, SysUserEntity.class);
        return sysUserService.registerUser(convert);
    }

    @ApiOperation(value = "用户列表查询", notes = "用户列表查询", httpMethod = "GET")
    @GetMapping("/list")
    public R getUserList(SysUserDto sysUserDto) {
        return sysUserService.getUserList(sysUserDto);
    }

    @SysLog(value = "用户删除")
    @ApiOperation(value = "用户删除", notes = "通过用户ID删除用户", httpMethod = "GET")
    @GetMapping("/removeUserById/{userId}")
    public R removeUserById(@ApiParam(name = "userId", value = "用户ID", required = true) @PathVariable String userId, HttpServletRequest request) {
        return sysUserService.removeUserById(userId, request);
    }

    @SysLog(value = "用户信息", addOrUpdate = true)
    @ApiOperation(value = "用户信息修改", notes = "用户信息修改", httpMethod = "POST")
    @PostMapping("/updateUser")
    public R addOrUpdateUser(@RequestBody SysUserModel sysUser) {
        SysUserEntity convert = BeanConvertUtils.convert(sysUser, SysUserEntity.class);
        return sysUserService.updateUser(convert);
    }

    @SysLog(value = "密码修改")
    @ApiOperation(value = "用户密码修改", notes = "用户密码修改", httpMethod = "POST")
    @PostMapping("/updatePassword")
    public R updateUserPassword(@RequestBody SysUserVo sysUserVo) {
        return sysUserService.updateUserPassword(sysUserVo);
    }

    @ApiOperation(value = "用户头像上传", httpMethod = "POST")
    @PostMapping("/uploadUserHead")
    public R uploadUerHead(@RequestParam("file") MultipartFile file, String userId) {
        return sysUserService.uploadUserHead(file, userId);
    }
}
